<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends MY_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index_bak.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */

    public $Cookie;

    public function __construct()
    {
        parent::__construct();
        $this->Cookie = $this->input->cookie();
    }

    public function test($page)
    {
        if ($page === 'upload') {
            $upload_file = iconv("UTF-8", "GB2312", $_FILES["file"]["name"]);
            if (file_exists("upload/" . $upload_file)) {
                //文件重名，文件已存在
                header($this->config->item('http_status')[500]);
                echo json_encode(array('result' => ERROR, 'msg' => '文件重名！', 'code' => 101));
            } else {
                $result = move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $upload_file);
                if ($result) {
                    //文件上传成功
                    echo json_encode(array('result' => SUCCESS, 'msg' => '文件上传成功！', 'code' => 0));
                } else {
                    header($this->config->item('http_status')[500]);
                    echo json_encode(array('result' => ERROR, 'msg' => '文件上传失败！', 'code' => 102));
                }
            }
            return true;
        } elseif ($page === 'xcx') {
            $upload_file = iconv("UTF-8", "GB2312", $_FILES["file"]["name"]);
            if (file_exists("upload/" . $upload_file)) {
                //文件重名，文件已存在
//                header($this->config->item('http_status')[500]);
                return true;
            } else {
                $result = move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $upload_file);
                if ($result) {
                    //文件上传成功
                    return true;
                } else {
                    header($this->config->item('http_status')[500]);
                    return false;
                }
            }
        }
//        $this->load->view("test/" . $page);
        return false;
    }

    public function xcx($page)
    {
        if ($page === 'zuowen') {
            $res = $this->test('xcx');
            if ($res) {//判断文件是否上传成功
                $upload_file = iconv("UTF-8", "GB2312", $_FILES["file"]["name"]);
                $_POST['file'] = $upload_file;
                $this->load->model('task/Task_zuowen_xcx_model', 'zw');
                $this->zw->add_task($_POST);
            }
        } elseif ($page === 'shufa') {
            $res = $this->test('xcx');
            if ($res) {//判断文件是否上传成功
                $upload_file = iconv("UTF-8", "GB2312", $_FILES["file"]["name"]);
                $_POST['file'] = $upload_file;
                $this->load->model('task/Task_shufa_xcx_model', 'zw');
                $this->zw->add_task($_POST);
            }
        } elseif ($page === 'aoshu') {
            $res = $this->test('xcx');
            if ($res) {//判断文件是否上传成功
                $upload_file = iconv("UTF-8", "GB2312", $_FILES["file"]["name"]);
                $_POST['file'] = $upload_file;
                $this->load->model('task/Task_aoshu_xcx_model', 'zw');
                $this->zw->add_task($_POST);
            }
        }
        return;
    }

    public function index()
    {
//		$this->load->view('welcome_message',$data);
        $this->load->view('index');
        $this->load->view('footer');
    }

    public function input()
    {
        $this->load->view('input');
    }

    public function dashboard()
    {
        $this->load->view('dashboard');
    }

    public function mark()
    {
        session_start();
        $cookie = $this->Cookie;
        //验证token
        if (isset($cookie['_wsxt']) && ver_token($cookie['_wsxt'], substr($cookie['uid'], -10))) {
            $data = array(
                'option' => '<option value="' . $cookie['sid'] . '">' . $cookie['name'] . '</option>'
            );
            $this->load->view('mark', $data);
            $this->load->view('message/dialog');
            $this->load->view('footer');
            return;
        }
        $this->index();
        return;
    }

    public function zuowen()
    {
        //验证token
        session_start();
        $cookie = $this->Cookie;
        if (isset($cookie['_wsxt']) && ver_token($cookie['_wsxt'], substr($cookie['uid'], -10))) {
            $data = array(
                'option' => '<option value="' . $cookie['sid'] . '">' . $cookie['name'] . '</option>'
            );
            $this->load->view('zuowen', $data);
            $this->load->view('message/dialog');
            $this->load->view('footer');
            return;
        }
        $this->index();
        return;
    }

    public function aoshu()
    {
        //验证token
        session_start();
        $cookie = $this->Cookie;
        if (isset($cookie['_wsxt']) && ver_token($cookie['_wsxt'], substr($cookie['uid'], -10))) {
            $data = array(
                'option' => '<option value="' . $cookie['sid'] . '">' . $cookie['name'] . '</option>'
            );
            $this->load->view('aoshu', $data);
            $this->load->view('message/dialog');
            $this->load->view('footer');
            return;
        }
        $this->index();
        return;
    }

    public function shufa()
    {
        //验证token
        session_start();
        $cookie = $this->Cookie;
        if (isset($cookie['_wsxt']) && ver_token($cookie['_wsxt'], substr($cookie['uid'], -10))) {
            $data = array(
                'option' => '<option value="' . $cookie['sid'] . '">' . $cookie['name'] . '</option>'
            );
            $this->load->view('shufa', $data);
            $this->load->view('message/dialog');
            $this->load->view('footer');
            return;
        }
        $this->index();
        return;
    }

    public function add_term($page)
    {
        $this->load->view('manage/' . $page);
    }
}